Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

282
Visualizações
Chrome Extension: "Unchecked runtime.lastError: The message port closed before a response was received"

I know this question at first reads like it's already been addressed on SO multiple times. But I face this issue in spite of all the other questions and answers.

Specifically, I know those 2 things are required (from other answers):

  • the message callback should return true when it is async
  • the sendMessage should always be used if it is expected (even just to resolve true)

I could not find any other gotchas that would make the following fail:

// content_script.js

const sendMessageToBackground = (payload) =>
    new Promise((resolve) => {
        chrome.runtime.sendMessage(payload, (response) => {
            resolve(response);
        });
    });

const request = { type: "papersWithCode" };
const backgroundResponse = await sendMessageToBackground(request); // triggers error
console.log(backgroundResponse) // undefined
// background.js

const dummy = async () => {
    const html = await fetch("https://stackoverflow.com/");
    return 42;
};

chrome.runtime.onMessage.addListener(async (payload, sender, sendResponse) => {
    if (payload.type === "papersWithCode") {
        const dum = await dummy();
        console.log("dum: ", dum); // 42
        sendResponse({ success: true, dum });
        return true;
    }
});

The problem seems to arise from the async nature of dummy() because if I change to the following it all works:

// background.js

const dummy = () => 42

chrome.runtime.onMessage.addListener(async (payload, sender, sendResponse) => {
    if (payload.type === "papersWithCode") {
        const dum = dummy();
        console.log("dum: ", dum); // 42
        sendResponse({ success: true, dum });
        return true;
    }
});
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda